﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using JSTT_DATA_ENTITY;
using JSTT_DATA_ENTITY.XMLDatas;

namespace JSTT_DATA_ENTITY
{
    public partial class TD_DATATYPE_FORMAT_RULES
    {
        private JSTT_DataRuleMatchs matchDataRule;
        private JSTT_DataRuleFormats formatDataRule;


        public JSTT_DataRuleFormats FormatDataRule
        {
            get { return formatDataRule; }
        }

        public JSTT_DataRuleMatchs MatchDataRule
        {
            get { return matchDataRule; }
        }

        /// <summary>
        /// 加载XML规则到对象中
        /// </summary>
        public void ReLoadRuleXml()
        {
            //var appSettings = System.Configuration.ConfigurationManager.AppSettings;
            //if (appSettings.Count == 0)
            //{
            //    return null;
            //}

            string xmlFilePath =
                System.Configuration.ConfigurationManager.AppSettings["XML_RULES_DICTIONARY"].ToString();

            matchDataRule = JSTT_DataRuleSerializer<JSTT_DataRuleMatchs>.ReadFile(xmlFilePath +this.DF_RULE_DATATYPE_MATCH);
            formatDataRule = JSTT_DataRuleSerializer<JSTT_DataRuleFormats>.ReadFile(xmlFilePath + this.DF_RULE_DATAS);

            if (matchDataRule == null)
                throw new ApplicationException(String.Format("Load XMLFile({0}) Failed.",this.DF_RULE_DATATYPE_MATCH));
            if(formatDataRule == null)
                throw new ApplicationException(String.Format("Load XMLFile({0}) Failed.", this.DF_RULE_DATAS));
            //JSTT_DataRuleSerializer<JSTT_DataRuleFormats> jsttDataRuleFormatSerializer = new JSTT_DataRuleSerializer<JSTT_DataRuleFormats>();
            //JSTT_DataRuleSerializer<JSTT_DataRuleMatchs> jsttDataRuleMatchSerializer = new JSTT_DataRuleSerializer<JSTT_DataRuleMatchs>();

            //matchDataRule = jsttDataRuleMatchSerializer
            //matchDataRule = jsttDataRuleMatchSerializer.Deserialize(this.DF_RULE_DATATYPE_MATCH);
            //formatDataRule = jsttDataRuleFormatSerializer.Deserialize(this.DF_RULE_DATAS);
        }



        /// <summary>
        /// 判断数据符合哪个规则
        /// </summary>
        /// <param name="bMessageBody"></param>
        /// <returns></returns>
        public bool IsDataMatchThisType(byte[] bMessageBody)
        {
            if (this.matchDataRule != null && matchDataRule.ToMatchData(bMessageBody))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 静态方法：获得所有数据规则
        /// </summary>
        /// <returns></returns>
        public static List<TD_DATATYPE_FORMAT_RULES> GetAllRules()
        {
            JSTT_DATA_ENTITY.JSTT_DBSEntities jsttEntity = new JSTT_DBSEntities();
            return jsttEntity.TD_DATATYPE_FORMAT_RULES.ToList();
        }
    }
}
